<?php

 global $_GPC, $_W;
wl_load()->model('setting');
 $config = tgsetting_load();
$member=pdo_fetch("SELECT id,total,from_user,parentid,enable FROM ".tablename('tg_member')." WHERE from_user='{$_W['openid']}' and uniacid='{$_W['uniacid']}'");	
		$member1=pdo_fetch("SELECT id,total,from_user,parentid FROM ".tablename('tg_member')." WHERE from_user='{$_W['openid']}' and enable=1  and uniacid='{$_W['uniacid']}'");	
		$member2=pdo_fetch("SELECT id,total,from_user,parentid FROM ".tablename('tg_member')." WHERE id='{$_GPC['sharenum']}'  and uniacid='{$_W['uniacid']}'");
		if(empty($member1))
		{
			$sharen=0;
		}else{
			$sharen=$member1['id'];
		}
		
		$to_url=app_url('order/mycashorder')."&sharenum=".$sharen;	
		$fans=pdo_fetch("SELECT * FROM ".tablename('mc_mapping_fans')." WHERE openid='{$_W['openid']}' and uniacid='{$_W['uniacid']}' and follow=1");	
		if((empty($member)||$member['parentid']==-1)&&!empty($_GPC['sharenum']))
		{
			//message(intval($_GPC['sharenum']));
			if($member['parentid']==-1&&!empty($fans))
			{
				$anum=0;
			}else{
				$anum=intval($_GPC['sharenum']);
			}
			$data = array(					
					'parentid' => $anum
				);
			pdo_update('tg_member', $data, array('id' =>$member['id']));
			
		}
		$member=pdo_fetch("SELECT id,total,from_user,parentid,type,shopname FROM ".tablename('tg_member')." WHERE from_user='{$_W['openid']}' and uniacid='{$_W['uniacid']}'");	
		if($member['type']==1)
		{
			$anumb=$member['id'];
		}else{
			$anumb=$member['parentid'];
		}
		
		if(empty($_GPC['sharenum'])&&$member['parentid']==-1)
		{
			if($member['parentid']==-1&&!empty($fans))
			{
				$anum=0;
			}else{
				$anum=intval($_GPC['sharenum']);
			}
			$data = array(					
					'parentid' => $anum
				);
			pdo_update('tg_member', $data, array('id' =>$member['id']));
		}
		$_Session['btitle']=$member['shopname'];
		$share_indexname=$_W['uniaccount']['name']."发布兼职招募令,速来!!!";
		$share_indexdesc="呼朋唤友一起干,动动手指来收益!";
		$share_images =tomedia($config['share']['share_image']);
	$op = intval($_GPC['op']); //op=0对应获取全部订单,op=1对应获取待付款订单,op=2对应获取待收货订单
	if(empty($_GPC['op']))
	{
		$op=1;
	}
	
	$openid = $_W['openid'];//用户的openid
	$weid=$_W['uniacid'];
	$member = pdo_fetch("SELECT * FROM " . tablename('tg_member') . " WHERE uniacid ='{$_W['uniacid']}' and from_user = '{$_W['openid']}'");
	if(empty($member['addtime']))
	{
		$member['addtime']=time();
	}
	//获取当前用户全部订单信息
	if($op==0){
		$sql = 'SELECT * FROM '.tablename('tg_order').' WHERE  uniacid = :uniacid  and comtype<>1 and ptime>:ptime and status not in (0,4,5,7,9,10) and openid in (select from_user from '.tablename('tg_member').' where parentid=:parentid) ORDER BY id desc'; //从订单信息表里面取得数据的sql语句
		
		$params = array(':parentid'=>$member['id'] , ':uniacid'=>$weid,':ptime'=>$member['addtime']);
		
		$orders = pdo_fetchall($sql, $params); 
		
		
	
	}elseif ($op==1) {	
	//获取当前用户待付款订单信息
$orders = pdo_fetchall("SELECT status,g_id,id,orderno,price,cost_fee,commission,tuan_id,createtime,commissiontype FROM " . tablename('tg_order') . " WHERE uniacid ='{$_W['uniacid']}' and status not in (0,4,5,7,9,10) and (comtype is NULL or comtype=0) and commission>0 and ptime>{$member['addtime']} and openid in (select from_user from ".tablename('tg_member')." where parentid={$member['id']}) order by id desc");
				
	}elseif ($op==2) {					//获取当前用户待收货订单信息 在数据表里status = 2代表待收货
		$orders = pdo_fetchall("SELECT g_id,id,orderno,price,cost_fee,commission,tuan_id,createtime,commissiontype FROM " . tablename('tg_order') . " WHERE uniacid ='{$_W['uniacid']}'  and  comtype=1 and commission>0  and ptime>{$member['addtime']}  and openid in (select from_user from ".tablename('tg_member')." where parentid={$member['id']})");

	}
	elseif ($op==3) 
	{
		
	}
	else{

		message('获取订单信息失败.', app_url('mycashorder',array('op' => '0')));

	}
$sumcom = pdo_fetchall("SELECT g_id,id,orderno,price,cost_fee,commission,tuan_id,createtime,commissiontype,gnum FROM " . tablename('tg_order') . " WHERE uniacid ='{$_W['uniacid']}' and status not in (0,4,5,7,9) and (comtype is NULL or comtype=0) and commission>0 and ptime>{$member['addtime']} and openid in (select from_user from ".tablename('tg_member')." where parentid={$member['id']})");
	
		
		$comp=0;
		foreach ($sumcom as $key => $value) {
			$price=$value['price']-$value['cost_fee'];
			if($value['g_id']>0)
{
	$sql3 = 'SELECT * FROM '.tablename('tg_goods').' WHERE id=:id';

                    $params3 = array(':id'=>$value['g_id']);

                    $good3 = pdo_fetch($sql3, $params3);

if($value['commissiontype']==2)
{$comp+=$value['commission'];}
else{$comp+=($value['price']-$value['cost_fee'])*$value['commission']/100;}

}else{
$sql3 = 'SELECT * FROM '.tablename('tg_collect').' WHERE uniacid=:uniacid and orderno=:orderno';
$params3 = array(':uniacid'=>$_W['uniacid'],'orderno'=>$value['orderno']);
 $favoriteqqq = pdo_fetchall($sql3, $params3);
	
				foreach ($favoriteqqq as $key1 => $orderss) 
					{				
						
						$sql3 = 'SELECT * FROM '.tablename('tg_goods').' WHERE id=:id';

                    $params3 = array(':id'=>$orderss['sid']);

                    $gs = pdo_fetch($sql3, $params3);
						if($gs['commissiontype']==2){$comp+=$gs['commission'];}
					     else{
							 $comp+=($orderss['oprice']*$orderss['num'])*$orderss['commission']/100;//佣金计算			
						 }			
					}			
}			
		}
		
if($member['enable']!=1)
{
	
		header("location: ".app_url('order/infojob'));
		exit;
	}else{
		include wl_template('order/mycashorder');
	}		


exit();

?>